package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.stark.translator.util.LanCodeUtil;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityPhotoTranslationBinding;
import flc.ast.dialog.LanguageDialog;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;
import stark.common.other.LanCode;
import stark.common.other.ReqManager;
import stark.common.other.bean.TranslateRet;

public class PhotoTranslationActivity extends BaseAc<ActivityPhotoTranslationBinding> {

    public static String sResult;
    private LanCode mCurSelLanCode;
    private static final int GEN_LAN_CODE_COUNT = 3;

    @Override
    protected int onCreate() {
        return R.layout.activity_photo_translation;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivPhotoTranslationBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivTranslation.setOnClickListener(this);
        mDataBinding.tvResultLan.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivTranslation://翻译文本
                translate();
                break;
            case R.id.tvResultLan://切换语言
                showLanguageDialog();
                break;
        }
    }

    @Override
    protected void initData() {
        List<LanCode> lanCodeList = LanCodeUtil.extractFromGeneralLanCodeList(GEN_LAN_CODE_COUNT);
        if (lanCodeList != null && lanCodeList.size() > 0) {
            mCurSelLanCode = lanCodeList.get(0);
            mDataBinding.tvResultLan.setText(mCurSelLanCode.getName());
        }
        mDataBinding.etRecResult.setText(sResult);
        translate();
    }

    private void translate() {
        String content = mDataBinding.etRecResult.getText().toString();
        if (TextUtils.isEmpty(content) || mCurSelLanCode == null) {
            return;
        }

        showDialog(getString(R.string.translating));
        ReqManager.getInstance().translate(this, content, LanCode.AUTO, mCurSelLanCode, new IReqRetCallback<TranslateRet>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable TranslateRet result) {
                dismissDialog();
                if (result != null) {
                    //将本次翻译成功的文本保存起来
                    mDataBinding.etTransResult.setText(result.getResult());
                } else {
                    ToastUtils.showShort(msg);
                }
            }
        });
    }

    private void showLanguageDialog() {
        LanguageDialog dialog = new LanguageDialog(mContext);
        dialog.mCurSelLanCode = mCurSelLanCode;
        dialog.setListener(new LanguageDialog.Ilistener() {
            @Override
            public void clickSelLan(LanCode lanCode) {
                mCurSelLanCode = lanCode;
                mDataBinding.tvResultLan.setText(lanCode.getName());
                translate();
            }
        });
        dialog.show();
    }

}